package assignment0.scanner;

import java.io.IOException;
import java.io.StringReader;

public class ScannerImpl implements Scanner {

	private String expression;
	private int counter;
	private boolean neverCalled;
	
	public ScannerImpl(String expressionInput) {
		this.expression = expressionInput;
		this.counter = 0;
		this.neverCalled = true;
	}
	
	public ScannerImpl(StringReader srInput) {
		expression = "";
		int mySRchar = 0;
		while (true) {
			try {
				mySRchar = srInput.read();
				if (mySRchar == -1)
					break;
				else
					expression += (char)mySRchar;
			} catch (IOException e) {
				System.out.println(e);
			}
		}
		this.counter = 0;
		this.neverCalled = true;
	}	
	
	@Override
	public char current() {
		this.neverCalled = false;
		return this.expression.charAt(counter);
	}

	@Override
	public char next() {
		if (neverCalled) {
			this.neverCalled = false;
			return this.current();
		} else {
			if (counter + 1 < this.expression.length()) {
				counter += 1;
				return this.current();
			} else {
				return EOF;
			}
		}
	}

	@Override
	public char peek() {
		if (neverCalled) {
			return this.expression.charAt(0);
		}
		if (counter + 1 < this.expression.length()) {
			return this.expression.charAt(counter + 1);
		} else {
			return EOF;
		}
	}
}
